import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:rembercount/controller/authLoginController.dart';
import 'package:rembercount/controller/countController.dart';
import 'package:rembercount/pages/loginscreen.dart';
import 'package:rembercount/pages/main_page.dart';
import '../controller/themeController.dart';
import '../pages/help_feedback_page.dart';
import '../pages/about_page.dart';

class ProfilePage extends StatelessWidget {
  // 主题控制器实例
  final ThemeController themeController = Get.find<ThemeController>();
  final Authlogincontroller authlogincontroller = Get.find<Authlogincontroller>();
  final CountController countController=Get.find<CountController>();
  

  ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 设置背景色为浅灰色
      backgroundColor: Colors.grey[100],
      // 顶部应用栏
      appBar: AppBar(
        title: const Text('我的'),
        centerTitle: true,
        elevation: 0,
        backgroundColor: themeController.themeColor,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            // 用户信息卡片
            _buildUserInfoCard(),
            const SizedBox(height: 12),
            // 功能列表
            // 设置列表
            _buildSettingsList(context),
          ],
        ),
      ),
    );
  }

  // 构建用户信息卡片
  Widget _buildUserInfoCard() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: themeController.themeColor,
        borderRadius: const BorderRadius.vertical(
          bottom: Radius.circular(20),
        ),
      ),
      child: Row(
        children: [
          // 用户头像
          CircleAvatar(
            radius: 40,
            backgroundColor: Colors.white,
            child: Icon(
              Icons.person,
              size: 50,
              color: themeController.themeColor,
            ),
          ),
          const SizedBox(width: 20),
          // 用户信息
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  authlogincontroller.emailController.text.isEmpty 
                      ? '未登录' 
                      : authlogincontroller.emailController.text,
                  style: const TextStyle(
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                  ),
                ),
                const SizedBox(height: 8),
                Text(
                  'ID: ${authlogincontroller.emailController.text.isEmpty ? '---' : authlogincontroller.emailController.text.hashCode}',
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.white.withOpacity(0.8),
                  ),
                ),
              ],
            ),
          ),
          // 编辑按钮
          IconButton(
            onPressed: () {
              Get.toNamed('/edit-profile');
            },
            icon: const Icon(
              Icons.edit,
              color: Colors.white,
            ),
          ),
        ],
      ),
    );
  }
   void showDialog(BuildContext context){
    print("object");
   return BrnDialogManager.showConfirmDialog(context,
    title: "重置账号",
    cancel: '取消',
    confirm: '确定',
    message: "是否重置该账号所以记账信息，成功后无法恢复", onConfirm: () async {
  var res=await countController.reset();
  print(res);
  if(res['code']==200){
    Get.snackbar("提示", "重置成功");
   
  }else{
    Get.snackbar("提示", res["msg"]);
   

  }
   Get.offAll(MainPage());
}, onCancel: () {
  Get.back();
});
 }

  // 构建功能列表
  Widget _buildFunctionList() {
    // 功能项数据
    final List<Map<String, dynamic>> functions = [
      {
        'icon': Icons.account_balance_wallet,
        'title': '我的钱包',
        'subtitle': '查看账户余额和交易记录',
        'onTap': () => Get.snackbar('提示', '钱包功能开发中'),
      },
      {
        'icon': Icons.star,
        'title': '我的收藏',
        'subtitle': '查看收藏的交易记录',
        'onTap': () => Get.snackbar('提示', '收藏功能开发中'),
      },
      {
        'icon': Icons.history,
        'title': '浏览历史',
        'subtitle': '查看最近浏览的记录',
        'onTap': () => Get.snackbar('提示', '历史记录功能开发中'),
      },
    ];

    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: functions.map((function) {
          return _buildListTile(
            icon: function['icon'],
            title: function['title'],
            subtitle: function['subtitle'],
            onTap: function['onTap'],
          );
        }).toList(),
      ),
    );
  }

  // 构建设置列表
  Widget _buildSettingsList(BuildContext context) {
    // 设置项数据
    final List<Map<String, dynamic>> settings = [
      {
        'icon': Icons.notifications,
        'title': '消息通知',
        'subtitle': '设置消息提醒方式',
        'onTap': () => Get.snackbar('提示', '通知设置功能开发中'),
      },
      {
        'icon': Icons.security,
        'title': '隐私设置',
        'subtitle': '管理隐私和安全选项',
        'onTap': () => Get.snackbar('提示', '隐私设置功能开发中'),
      },
      {
        'icon': Icons.restore,
        'title': '重置账号',
        'subtitle': '重置账户的记账信息',
        'onTap': () => showDialog(context),
      },
      {
        'icon': Icons.money,
        'title': '预算设置',
        'subtitle': '设置每个月的使用预算',
        'onTap': () => showpreFundDialog(context),
      },
      {
        'icon': Icons.help,
        'title': '帮助与反馈',
        'subtitle': '常见问题解答和意见反馈',
        'onTap': () => Get.to(() => HelpFeedbackPage()),
      },
      {
        'icon': Icons.info,
        'title': '关于我们',
        'subtitle': '版本信息和开发者信息',
        'onTap': () => Get.to(() => AboutPage()),
      },
      {
        'icon': Icons.logout,
        'title': '退出登录',
        'subtitle': '',
        'onTap': () => Get.to(() => LoginScreen()),
      },
    ];

    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: settings.map((setting) {
          return _buildListTile(
            icon: setting['icon'],
            title: setting['title'],
            subtitle: setting['subtitle'],
            onTap: setting['onTap'],
          );
        }).toList(),
      ),
    );
  }
 Future<void> showpreFundDialog(BuildContext context) async {
  final preFund= await authlogincontroller.getPreFund();
  return BrnMiddleInputDialog(
  title: '月度预算设置',
  message: "仅可输入月度金额,当前的月度预算：${preFund ?? ''}",
  hintText: '请输入月度预算金额',
  cancelText: '取消',
  confirmText: '确定',
  autoFocus: true,
  keyboardType: TextInputType.number,
  maxLength: 1000,
  maxLines: 2,
  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
  dismissOnActionsTap: false,
  barrierDismissible: true,
  onConfirm: (value) {
    authlogincontroller.preFund=double.parse(value) ;
  authlogincontroller.setPreFund();
   
    
    Navigator.pop(context);
  },
  onCancel: () {
    BrnToast.show("取消", context);
    Navigator.pop(context);
  }).show(context);
 }
  // 构建列表项
  Widget _buildListTile({
    required IconData icon,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: Colors.grey.withOpacity(0.1),
                width: 1,
              ),
            ),
          ),
          child: Row(
            children: [
              // 图标
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: themeController.themeColor.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Icon(
                  icon,
                  color: themeController.themeColor,
                  size: 24,
                ),
              ),
              const SizedBox(width: 16),
              // 标题和副标题
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    const SizedBox(height: 4),
                    Text(
                      subtitle,
                      style: TextStyle(
                        fontSize: 12,
                        color: Colors.grey[600],
                      ),
                    ),
                  ],
                ),
              ),
              // 箭头图标
              Icon(
                Icons.chevron_right,
                color: Colors.grey[400],
              ),
            ],
          ),
        ),
      ),
    );
  }
} 